package caro.client.controller;

import caro.app.bussiness.UserSession;
import caro.client.gui.Login;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import caro.app.protocolprovider.*;
import caro.client.gui.MainInterface;
import caro.client.gui.Messages;
import java.awt.Cursor;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.swing.JOptionPane;

/**
 * Class LoginController
 * @author Thanh Nhan
 * @Since Aug 30, 2011
 * @Version 1.0
 */
public class LoginController {
    private Login loginView = null;
    private UserSession session = null;
    public LoginController(Login loginView){
        this.loginView = loginView;
        this.loginView.addLoginListener(new LoginListener());
        this.loginView.addCancelListener(new CancelListener());
        session = new UserSession(null);
    }
    class CancelListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            loginView.getParent().show(true);
            loginView.dispose();
        }
        
    }
    class LoginListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            final Waiting w = new Waiting(loginView);
            w.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            new Thread(){
                @Override
                public void run(){
                    w.setVisible(true);
                }
            }.start();
            new Thread(new Runnable() {

                @Override
                public void run() {
                    PrintWriter pw = null;
                    BufferedReader br = null;
                    try {
                        String line = ProtocolProcessing.getAuth(loginView.getUsername(), loginView.getPassword());
                        pw = new PrintWriter(loginView.getSocket().getOutputStream());
                        pw.println(line);
                        pw.flush();
                        br = new BufferedReader(new InputStreamReader(loginView.getSocket().getInputStream()));
                        String rs = br.readLine();
                        LoginSuccessPro success = ProtocolProcessing.explainLoginSuccess(rs);
                        if(success != null){
                            session.setMyUsername(loginView.getUsername());
                            MainInterface mainView = new MainInterface(session, loginView.getSocket(), success.userFree, success.userBusy);
                            MainInterfaceCtrl mainInterfaceCtrl = new MainInterfaceCtrl(mainView);
                            mainInterfaceCtrl.start();
                            mainView.setVisible(true);
                            loginView.dispose();
                            System.out.println(success.userFree);
                            System.out.println(success.userBusy);
                        }else{
                            JOptionPane.showMessageDialog(loginView, Messages.getString("loginFail")
                                    , Messages.getString("errorTitle"), JOptionPane.ERROR_MESSAGE);
                        }
                        w.dispose();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }).start();
        }
        
    }
}
